package logic

import (
	"context"
	"fmt"
	"user666/model/mysql"
	"user666/model/redis"

	"user666/userrpc/internal/svc"
	"user666/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LonginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLonginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginLogic {
	return &LonginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LonginLogic) Longin(in *userrpc.LonginRequest) (*userrpc.LonginResponse, error) {
	// todo: add your logic here and delete this line
	get := redis.Client.Get("SendSmslongin" + in.Username)
	if get.Val() != in.Send {
		return nil, fmt.Errorf("验证码错误，请重新输入")
	}
	var finduser mysql.User
	mysql.DB.Where("username=?", in.Username).Find(&finduser)
	if finduser.ID == 0 {
		return nil, fmt.Errorf("该用户还没注册,请先去注册")
	}
	if finduser.Password != in.Password {
		return nil, fmt.Errorf("密码输入错误,请重新输入")
	}
	redis.Client.Del("SendSmslongin" + in.Username)
	return &userrpc.LonginResponse{Id: int64(finduser.ID)}, nil
}
